/*********************************************************************************************************************/
/*RESSOURCES MANAGEMENT***********************************************************************************************/
/*********************************************************************************************************************/

function ResourceManager(){

	//load images from URL
	this.loadImg = function(){
		/*function that permits to load images : later it should be divided in several script and
		be loaded asynchronously through the library requireJS*/
		
		//URL for loading images. -> To stock in a xml please...
		var tileDict = Array("","road.png","grass.png","dirt.png","savanna.png");
		var tileOverDict = Array("","road-ne-se.png","road-sw-nw.png","road-n-ne.png","road-s-sw.png","road-test.png","road-ne.png","savanna-s-sw-nw.png","savanna-sw.png","savanna-s-sw.png","savanna-s.png");
		var monsterDict = Array("","whiteMage.png","thief.png","paladin.png","specter.png","shyde.png");
		var buildingDict = Array("","house.png");
		
		//loading bar
		var loadingBar = new LoadBar();
		var totalToLoad = tileDict.length + tileOverDict.length + monsterDict.length + buildingDict.length - 4;
		loadingBar.setUp(totalToLoad);
		
		//loading tiles
		loadDictionnary(tileDict, tileImg, "tiles", loadingBar);
		//loading tiles over
		loadDictionnary(tileOverDict, tileOverImg, "overtiles", loadingBar);
		//loading monsters
		loadDictionnary(monsterDict, monsterImg, "monsters", loadingBar);
		//loading buildings
		loadDictionnary(buildingDict, buildingImg, "buildings", loadingBar);
	}
	
	function loadDictionnary(dict, arrayImg, path, loadingBar){
		for(var i=1;i<dict.length;i++){
			arrayImg[i] = new Image();
			arrayImg[i].src = "../../ressources/images/" + path + "/" + dict[i];
			arrayImg[i].onload = function(){
				loadingBar.update();
			}
		}
	}
	
	//class private loadBar
	function LoadBar(){
		var loaded;
		var totalToLoad;
		var loadBarDisplay = document.getElementById('loadBar');
		
		this.setUp = function(total) {
			loaded = 0;
			totalToLoad = total;
			//start display.. dispatch to the displayer
		}
		
		this.update = function(){
			loaded++;
			//display update... loadBar.value = loaded + "/" + totalToLoad;
			if (loaded == totalToLoad) {
				//HACK SHOULDNT BE HERE ???
				game.start();
				
				loadBarDisplay.hidden=true;
			}
		}
	}
	
}

/*********************************************************************************************************************/
/*CLASS MAP***********************************************************************************************************/
/*********************************************************************************************************************/

//class public map
function Map(){
	var tile;
	var tileOver;
	var monster;
	var building;
}

/*********************************************************************************************************************/
/*XML MANAGEMENT******************************************************************************************************/
/*********************************************************************************************************************/

//Class that manages transformation between XML and map array

function XMLManager() {
	
	//load the xml file
	this.loadXML = function (fileName) {
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest(); //FF
		}else if(window.ActiveXObject){
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE
		}
		xmlhttp.open("GET",fileName,false);
		xmlhttp.send();
		xmlDoc=xmlhttp.responseXML;
	};
	 
	 //get content ex getContent('tiles')
	this.getContent = function(name){
		var tempArray = Array();
		var generatedArray = Array();
		var row;
		var toGet = xmlDoc.getElementsByTagName(name)[0];
		for(var i=0;i<toGet.getElementsByTagName("row").length;i++){
			var rowSplit = toGet.getElementsByTagName("row")[i].childNodes[0].nodeValue;
			rowSplit = rowSplit.split(",");
			for(var j=0;j<rowSplit.length;j++){
				tempArray.push(rowSplit[j]);
			}
			generatedArray.push(tempArray);
			tempArray =[];
		}
		return generatedArray;
	};
	 
	//get attribute of a node, for ex getAttribute(map,id= return 1 for map1.
	this.getAttribute = function(name,attribute){
		var toGet = xmlDoc.getElementsByTagName(name)[0];
		attribute = toGet.attributes.getNamedItem(attribute).value;
		return attribute;
	};
}